package cms.mager.service.impl;


import cms.common.CodeMsg;
import cms.common.PageData;
import cms.common.Result;
import cms.common.SqlSessionAndMapper;
import cms.mager.domain.User;
import cms.mager.mapper.UserMapper;
import cms.mager.service.UserService;
import cms.util.SqlSessionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/8 17:02
 */
public class UserServiceImpl implements UserService {

    @Override
    public Result Login(String name, String pwd) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        try{
            User user= mapper.getObj().findOne(name,pwd);
            SqlSessionUtil.commitAndclose(mapper.getSqlSession());
            if(user == null){
                rt = new Result(CodeMsg.ERROR_USER_NOTFOUND); //此用户不存在
            }else{
                rt = new Result(user); //正确响应
            }
        }catch(Exception e){
            rt = new Result(CodeMsg.ERROR_USER_NAME_PWD); //用户名或密码不正确
            e.printStackTrace();
        }
        return rt;
    }

    @Override
    public Result findUserPage(Integer page,Integer pagesize,User user) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        try{
            Page<User> objects = PageHelper.startPage(page, pagesize);
            mapper.getObj().findUserPage(user);
            PageData<User> customerPageData = new PageData<>();
            customerPageData.setPage(page);
            customerPageData.setPageSize(pagesize);
            customerPageData.setPageCount((int)objects.getTotal());
            customerPageData.setPageNum(objects.getPages());
            customerPageData.setLis(objects.getResult());
            rt = new Result(customerPageData);
            SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        }catch(Exception e){
            e.printStackTrace();
            rt = new Result(CodeMsg.ERROR_SERVER_BUG);
            return rt;
        }
        return rt;
    }

    /**
    * 功能描述: 全查询用户数据
    * @Author: yuhui  2021/4/8 17:28
    */
    public Result findAllUser(){
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result re = null;
        try{
            List<User> all = mapper.getObj().findAll();
            re = new Result(all);
            SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        }catch(Exception e){
            e.printStackTrace();
            re = new Result(CodeMsg.ERROR_SERVER_BUG);
            return re;
        }
        return re;
    }

    @Override
    public Result adduser(User user) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        if(mapper.getObj().addData(user) != 0){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_ADD_USER);
        }
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return rt;
    }

    @Override
    public Result reSetPwd(int id, String pwd, Date mofyDate) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        if(mapper.getObj().reset(id,pwd,mofyDate) != 0){
            rt = new Result();
        }else {
            rt = new Result(CodeMsg.ERROR_SET_USER);
        }
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return rt;
    }

    @Override
    public Result delInfoById(int id,Date delDate) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        if(mapper.getObj().delUserById(id,delDate) != 0){
            rt = new Result();
        }else {
            rt = new Result(CodeMsg.ERROR_DEL_USER);
        }
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return rt;
    }

    @Override
    public Result getAllName() {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        List<User> allName = mapper.getObj().getAllName();
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return new Result(allName);
    }

    @Override
    public Result updPwd(String id,String newPwd,Date mofyDate) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        if(mapper.getObj().reset(Integer.valueOf(id),newPwd,mofyDate) != 0){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_MODIFY_IMG);
        }
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return rt;
    }

    @Override
    public Result updImg(String imgFile, Integer id) {
        SqlSessionAndMapper<UserMapper> mapper = SqlSessionUtil.getMapper(UserMapper.class);
        Result rt = null;
        if(mapper.getObj().updImg(id,imgFile) != 0){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_SERVER_BUG);
        }
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return rt;
    }

}
